package org.keyroy.util.file;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Vector;

import javax.swing.JFileChooser;

import org.keyroy.tag.Tag;
import org.keyroy.tag.XmlSupport;

public class FileUtil {
	public static final String INI_NAME = "config.ini";

	public static final void makeFolder(File folder) {
		if (folder.exists() == false) {
			folder.mkdirs();
		}
	}

	public static final JFileChooser getFileChooser(String title, javax.swing.filechooser.FileFilter fileFilter) {
		JFileChooser fileChooser = new JFileChooser();
		fileChooser.setFileFilter(fileFilter);
		fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
		fileChooser.setDialogTitle(title);
		return fileChooser;
	}

	public static final void save(File root, XmlSupport xmlSupport) {
		File file = new File(root, xmlSupport.getTagName());

		try {
			String text = new Tag(xmlSupport).toString();
			file.createNewFile();
			FileOutputStream fileOutputStream = new FileOutputStream(file);
			fileOutputStream.write(text.getBytes("utf-8"));

			System.out.println("save file = : " + file.getAbsolutePath() + "\n" + text);

			fileOutputStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static final void load(File root, XmlSupport xmlSupport) throws Exception {
		File file = new File(root, xmlSupport.getTagName());
		FileInputStream fileInputStream = new FileInputStream(file);
		Tag tag = new Tag(fileInputStream);
		xmlSupport.initObj(tag);

	}

	public static final void writeIni(String title, String param) {
		try {

			File config = getIni();
			if (config.exists() == false) {
				config.createNewFile();
			}

			FileInputStream fileInputStream = new FileInputStream(config);
			String text = title + "=" + param;
			List<String> list = new Vector<String>();
			list.add(text);
			while ((text = readLine(fileInputStream)) != null) {
				if (text.startsWith(title + "=")) {
				} else {
					list.add(text);
				}
			}

			FileOutputStream fileOutputStream = new FileOutputStream(config);
			for (int i = 0; i < list.size(); i++) {
				fileOutputStream.write(list.get(i).getBytes());
				fileOutputStream.write('\r');
				fileOutputStream.write('\n');
			}
			fileOutputStream.close();
		} catch (Exception e) {
		}
	}

	public static final void deleteIni(String title) {
		try {
			File config = getIni();
			if (config.exists() == false) {
				config.createNewFile();
			}
			FileInputStream fileInputStream = new FileInputStream(config);
			String text = null;
			List<String> list = new Vector<String>();
			while ((text = readLine(fileInputStream)) != null) {
				if (text.startsWith(title + "=")) {
				} else {
					list.add(text);
				}
			}

			FileOutputStream fileOutputStream = new FileOutputStream(config);
			for (int i = 0; i < list.size(); i++) {
				fileOutputStream.write(list.get(i).getBytes());
				fileOutputStream.write('\r');
				fileOutputStream.write('\n');
			}
			fileOutputStream.close();
		} catch (Exception e) {
		}
	}

	public static final String readLine(InputStream inputStream) {

		try {
			ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
			int c = 0;
			while ((c = inputStream.read()) != -1) {
				if (c == '\r') {
					return new String(arrayOutputStream.toByteArray());
				} else if (c != '\n') {
					arrayOutputStream.write(c);
				}
			}
		} catch (Exception e) {
		}

		return null;
	}

	public static final String readIni(String title) {
		try {
			File config = getIni();
			if (config.exists()) {
				FileInputStream fileInputStream = new FileInputStream(config);

				String text = null;
				while ((text = readLine(fileInputStream)) != null) {
					if (text.startsWith(title + "=")) {
						return text.substring((title + "=").length(), text.length());
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	public static final File getRootFile() {
		File folder = new File(System.getProperty("user.dir"));
		return folder;
	}

	public static final File getIni() {
		File folder = FileUtil.getRootFile();
		File config = new File(folder, INI_NAME);
		return config;
	}
}
